package com.qf.compusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.compusdemo.dao.FbDao;
import com.qf.compusdemo.dao.UserDao;
import com.qf.compusdemo.entity.Fb;
import com.qf.compusdemo.entity.User;
import com.qf.compusdemo.entity.Wxs;
import com.qf.compusdemo.service.FbService;
import com.qf.compusdemo.service.WxsService;
import com.qf.compusdemo.vo.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/normal")
public class NormalController {



    @Autowired
    private FbService fbService;

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private  WxsService wxsService;;

    @RequestMapping("/fk")
    @ResponseBody
    public MessageVo test0(HttpSession session,FFB bb) throws IOException {
        String filename=bb.getImgss().getOriginalFilename();
        String unique= UUID.randomUUID().toString();
        String ext = FilenameUtils.getExtension(filename);
        String uniqueFileName=unique+"."+ext;
        System.out.println("唯一的文件名："+uniqueFileName);

        String real_path = session.getServletContext().getRealPath("/upload_file");

        File file=new File(real_path);
        if(!file.exists()){
            file.mkdirs();
        }
        String ggmessage=bb.getGgmessage();
        String username=(String)session.getAttribute("username");
        Date time=new Date();
        Fb ff=new Fb(null,ggmessage,"/upload_file/"+uniqueFileName,username,time,bb.getTypes(),0);
        int a=fbService.insertFb(ff);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a==1?"success":"fail");
        message.setData(ff);
        bb.getImgss().transferTo(new File(real_path+"\\"+uniqueFileName));
        return message;

    }

    @RequestMapping("/wx")
    @ResponseBody
    public MessageVo test1(HttpSession session, Wx bb) throws IOException {
        String filename=bb.getImgss().getOriginalFilename();
        String unique= UUID.randomUUID().toString();
        String ext = FilenameUtils.getExtension(filename);
        String uniqueFileName=unique+"."+ext;
        System.out.println("唯一的文件名："+uniqueFileName);

        String real_path = session.getServletContext().getRealPath("/upload_file");

        File file=new File(real_path);
        if(!file.exists()){
            file.mkdirs();
        }
        String ggmessage=bb.getGgmessage();
        String phonenum=bb.getPhonenum();
        String username=(String)session.getAttribute("username");
        Date time=new Date();
        Wxs wxx=new Wxs(null,ggmessage,"/upload_file/"+uniqueFileName,phonenum,username,time,bb.getTypes());

        int a=wxsService.insertWxs(wxx);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a==1?"success":"fail");
        message.setData(wxx);
        bb.getImgss().transferTo(new File(real_path+"\\"+uniqueFileName));
        return message;

    }

    @RequestMapping("/allfk")
    @ResponseBody
    public tableVo<Fb> test3(HttpServletRequest request,Ppage pp){

        Page page=new Page(pp.getPage(),pp.getLimit());
        String uname=(String)request.getSession().getAttribute("username");
        IPage<Fb> ipage =fbService.getPages(page,uname);
        tableVo<Fb> table=new tableVo<>();
        table.setCode("0");
        table.setMsg("ok");
        table.setData(ipage.getRecords());
        table.setCount(ipage.getTotal());
        System.out.println(table);
        return table;


    }


    @RequestMapping("/del")
    @ResponseBody
    public MessageVo test4(Integer[] ids){

        int a=fbService.delFb(ids);

        MessageVo message=new MessageVo();
        message.setStatus(a>0?true:false);
        message.setMessage(a>0?"success":"fail");
        return message;
    }



}
